package org.nowcoder.leetcode.greedy;

import java.util.Arrays;

/**
 * Title  : 1005. Maximize Sum Of Array After K Negations
 * Source : https://leetcode.cn/problems/maximize-sum-of-array-after-k-negations/description/
 * Author : XrazYang
 * Date   : 2024-2-3
 */

public class LeetCode_1005 {
    public int largestSumAfterKNegations(int[] nums, int k) {
        int sum = Arrays.stream(nums).sum();
        Arrays.sort(nums);
        for (int i = 0; i < k; i++) {
            sum = sum - nums[0] * 2;
            nums[0] = -nums[0];
            Arrays.sort(nums);
        }
        return sum;
    }
}
